package com.frikinet.tools.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ListUtils
{
	public static String join(String separator, List<? extends Object> list)
	{
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<list.size();i++)
		{
			sb.append(list.get(i).toString());
			if(i<list.size()-1)
				sb.append(separator);
		}
		return sb.toString();
	}
	
	public static String join(String separator, List<? extends Object> list, String propertyFunction)
	{
		List<Object> propList = new ArrayList<Object>();
		if(list.size() > 0)
		{
			Class<? extends Object> clazz = list.get(0).getClass();
			try
			{
				Method function = clazz.getMethod(propertyFunction);
				for(Object o: list)
				{
					propList.add(function.invoke(o));
				}
			}
			catch(Exception e)
			{
				
			}
		}
		return join(separator, propList);
	}
}
